var JsBridge = {
    /*2017.03.13,rl,old*/
    /*
     connectWebViewJavascriptBridge: function (callback) {
     if (window.WebViewJavascriptBridge) {
     callback(WebViewJavascriptBridge);
     } else {
     document.addEventListener('WebViewJavascriptBridgeReady', function () {
     callback(WebViewJavascriptBridge);
     }, false);
     }
     },
     JsBridgeInit: function (callback) {
     this.connectWebViewJavascriptBridge(function (bridge) {
     bridge.init(function (message, responseCallback) {
     //log('JS got a message', message);
     var data = {'Javascript Responds': 'Wee!'};
     //log('JS responding with', data);
     responseCallback(data);
     });
     });
     callback('done');//回调处理
     },
     */

    setupWebViewJavascriptBridge: function (callback) {
        var that = this;
        if (window.WebViewJavascriptBridge) {
            return callback(WebViewJavascriptBridge);
        } else {
            document.addEventListener(
                'WebViewJavascriptBridgeReady'
                , function () {
                    callback(WebViewJavascriptBridge)
                },
                false
            );
        }
        if (window.WVJBCallbacks) {
            return window.WVJBCallbacks.push(callback);
        }
        window.WVJBCallbacks = [callback];
        that.WVJBIframe = document.createElement('iframe');
        that.WVJBIframe.style.display = 'none';
        that.WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__';
        document.documentElement.appendChild(that.WVJBIframe);
        /*setTimeout(function () {
         document.documentElement.removeChild(WVJBIframe)
         }, 0);*/
    },

    JsBridgeInit: function (callback) {
        this.setupWebViewJavascriptBridge(function (bridge) {
            bridge.init(function (message, responseCallback) {
                console.log('JS got a message', message);
                var data = {
                    'Javascript Responds': 'Wee!'
                };
                console.log('JS responding with', data);
                responseCallback(data);
            });
        });
        /*
         setTimeout(function () {
         callback('done');//回调处理
         }, 100);
         */
    },

    //获取用户登录信息
    getUserInfo: function (data, callback) {
        if (typeof callback !== 'function') {
            callback = false;
        }
        WebViewJavascriptBridge.callHandler(
            'getUserInfo',
            data || {},
            function (responseData) {
                if (callback && responseData) {
                    callback(responseData);
                }
            }
        )
    },
    //通知app以native方式打开新h5页面
    openNewPageH5: function (data, callback) {
        if (typeof callback !== 'function') {
            callback = false;
        }
        WebViewJavascriptBridge.callHandler(
            'openNewPageH5',
            data || {},
            function (responseData) {
                if (callback && responseData) {
                    callback(responseData);
                }
            }
        )
    },

    //通知app跳转新native页面
    openNewPageNative: function (data, callback) {
        if (typeof callback !== 'function') {
            callback = false;
        }
        WebViewJavascriptBridge.callHandler(
            'openNewPageNative',
            data || {},
            function (responseData) {
                if (callback && responseData) {
                    callback(responseData);
                }
            }
        )
    },

    //通知native刷新购物车
    refreshCart: function (data, callback) {
        if (typeof callback !== 'function') {
            callback = false;
        }
        WebViewJavascriptBridge.callHandler(
            'refreshCart',
            data || {},
            function (responseData) {
                if (callback && responseData) {
                    callback(responseData);
                }
            }
        )
    },

    //通知native刷新订单列表
    refreshOrderList: function (data, callback) {
        if (typeof callback !== 'function') {
            callback = false;
        }
        WebViewJavascriptBridge.callHandler(
            'refreshOrderList',
            data || {},
            function (responseData) {
                if (callback && responseData) {
                    callback(responseData);
                }
            }
        )
    },

    //当前页面状态发生改变，通知native刷新当前页面
    refreshCurrentPage: function (data, callback) {
        if (typeof callback !== 'function') {
            callback = false;
        }
        WebViewJavascriptBridge.callHandler(
            'refreshCurrentPage',
            data || {},
            function (responseData) {
                if (callback && responseData) {
                    callback(responseData);
                }
            }
        )
    },

    //通知native关闭当前H5页面
    closeWebView: function (data, callback) {
        if (typeof callback !== 'function') {
            callback = false;
        }
        WebViewJavascriptBridge.callHandler(
            'closeWebView',
            data || {},
            function (responseData) {
                if (callback && responseData) {
                    callback(responseData);
                }
            }
        )
    },

    //打开主页页面
    openIndexPage: function (data, callback) {
        if (typeof callback !== 'function') {
            callback = false;
        }
        WebViewJavascriptBridge.callHandler(
            'openIndexPage',
            data || {},
            function (responseData) {
                if (callback && responseData) {
                    callback(responseData);
                }
            }
        )
    },

    //js获取从native传递过来的数据
    GetMsgFromNative: function (callback) {
        if (typeof callback !== 'function') {
            callback = false;
        }
        WebViewJavascriptBridge.registerHandler('GetMsgFromNative', function (data, response) {
            if (data && callback) {
                callback(data);
            }
            //response.respondWith({ 'Javascript Says':'Right back atcha!' });
        })
    }
};

